package com.hujh.android.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.hujh.android.R;

public class ImageViewAdapter extends BaseAdapter {

   // 定义Context
	private Context	mContext;
	// 定义整型数组 即图片源
	private Integer[] mImageIds	= { 
			R.drawable.m1, 
			R.drawable.m2, 
			R.drawable.m3, 
			R.drawable.m4, 
			R.drawable.m5, 
			R.drawable.m6, 
			R.drawable.m7,
			R.drawable.m8, 
			R.drawable.m9
	};
	
	public ImageViewAdapter(Context c){
		mContext = c;
	}
	
	public ImageViewAdapter(Context c, Integer[] mi){
		mContext = c;
		mImageIds = mi;
	}
	
	@Override
	// 获取图片的个数
	public int getCount() {
		return mImageIds.length;
	}

	@Override
	// 获取图片在库中的位置
	public Object getItem(int position) {
		 return position;
	}

	@Override
	// 获取图片ID
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null){
			// 给ImageView设置资源
			imageView = new ImageView(mContext);
			// 设置布局 图片120×120显示
			imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
			// 设置显示比例类型
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			
		}else{
			imageView = (ImageView) convertView;
		}
		imageView.setImageResource(mImageIds[position]);
		return imageView;
	}

}
